﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace GenerateurMusique
{
    class population
    {
        public individu[] individus = new individu[10];
        double tauxmutation;
        double tauxcrossover;
        int generation;

        public population(double mutation, double crossover)
        {
            this.tauxmutation = mutation;
            this.tauxcrossover = crossover;
            generation = 0;
            Random rand = new Random();
            for (int i = 0; i < 10; i++)
            {
                int intrumenttemp = rand.Next(1, 129);
                int[] notestemp = new int[20];
                for (int j = 0; j < 20; j++)
                {
                    notestemp[j] = rand.Next(24, 96);
                }
                individus[i] = new individu(intrumenttemp, notestemp);
                individus[i].CreateAndPlayMusic(i);
            }
        }
        int getgeneration()
        {
            return(this.generation);
        }
        public void selection(int[] notes ){
            Random rand = new Random();
           individu[] poptemp=new individu[10];
            var Roue = new List<individu>();
            for (int i = 0; i < individus.Length; i++)
            {
                for (int j=0;j<notes[i];j++){
                    Roue.Add(individus[i]);
                }

            }
           
            for (int i = 0; i < 10; i++)
            {
                int position=rand.Next(0,Roue.Count);
                poptemp[i]=Roue[position];
                Roue.Remove(Roue[position]);
            }
            for (int i = 0; i < poptemp.Length; i++)
            {
                Debug.WriteLine(poptemp[i].instrument);

            }
            individus = poptemp;
            testcrossover(rand);
        }
        public void testcrossover(Random rand)
        {
            for(int i=0;i<individus.Length;i++){
            if (rand.Next(0, 2) == 1)
            {
                individus[i] = new individu(individus[i], individus[rand.Next(0,individus.Length)], tauxcrossover, tauxmutation);
            }
            else {
                individus[i] = new individu(individus[i], tauxmutation);
            }
            }
        }
    }
}
